/*
 * @(#)ServiceExecutorTestCase.java $version 2010. 8. 11
 *
 * Copyright 2007-2010 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nhncorp.usf.core.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mock.web.MockServletContext;

import com.nhncorp.lucy.common.config.model.ApplicationInfo;
import com.nhncorp.lucy.common.container.SpringBeanFactory;
import com.nhncorp.usf.core.config.ConnectorInfos;
import com.nhncorp.usf.core.config.runtime.Action;
import com.nhncorp.usf.core.config.runtime.BusinessObject;
import com.nhncorp.usf.core.config.runtime.BusinessObjectGroup;
import com.nhncorp.usf.core.config.runtime.Method;
import com.nhncorp.usf.core.config.runtime.ParameterInfo;
import com.nhncorp.usf.core.config.runtime.ScriptEngineGlobals;
import com.nhncorp.usf.core.config.runtime.ScriptInfo;
import com.nhncorp.usf.core.config.runtime.ServiceCondition;
import com.nhncorp.usf.core.exception.DefaultExceptionHandler;
import com.nhncorp.usf.core.exception.ExceptionHandler;
import com.nhncorp.usf.core.invoker.ConnectorHolder;
import com.nhncorp.usf.core.invoker.InvocationConnectorManager;
import com.nhncorp.usf.core.invoker.Invoker;
import com.nhncorp.usf.core.invoker.SpringInvoker;
import com.nhncorp.usf.core.service.ServiceExecutor.RESULT_STATUS;
import com.nhncorp.usf.core.service.supplement.ParameterGenerator;
import com.nhncorp.usf.core.service.supplement.PreSupplement;
import com.nhncorp.usf.core.servlet.ServletContextHolder;
import com.nhncorp.usf.core.util.ScriptUtil;

/**
 * {@link ServiceExecutor} TestCase.
 *
 * @author Web Platform Development Team
 */
public class ServiceExecutorTestCase {
    static ServiceExecutor serviceExecutor = new DefaultServiceExecutor();
    static javax.script.ScriptEngineManager scriptEngineManager = new javax.script.ScriptEngineManager();
    static Action actionInfo;
    Log log = LogFactory.getLog(ServiceExecutorTestCase.class);

    @BeforeClass
    public static void initialize() throws Exception {

        ApplicationInfo.setFileEncoding("utf-8");
        Invoker invoker = new SpringInvoker();
        ConnectorHolder connectorHolder = new ConnectorHolder(invoker);
        SpringBeanFactory.init(new ClassPathXmlApplicationContext("applicationContext-bo.xml"));
        ExceptionHandler exceptionHandler = new DefaultExceptionHandler();
        serviceExecutor.setInvoker(invoker);
        serviceExecutor.setExceptionHandler(exceptionHandler);

        InvocationConnectorManager.getInstance().putInvoker(
                "localSpring",
                connectorHolder
        );

        ConnectorInfos.getInstance().setDefaultConnector("localSpring");
        ServletContext servletContext = new MockServletContext();
        ServletContextHolder.set(servletContext);
        String scriptDirPath = servletContext.getRealPath("/scripts");
        ServletContextHolder.setContextRealPath(scriptDirPath);
        ScriptUtil.loadJSFile(scriptDirPath);
    }

    @AfterClass
    public static void destroy() throws Exception {
        SpringBeanFactory.clear();
        InvocationConnectorManager.getInstance().putInvoker("localSpring", (Invoker) null);
    }

    /**
     * 기본 처리 방식 테스트
     *
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
	@Test
    public void simpleServiceExecute() throws Exception {
        actionInfo = new Action("dummy_action", "dummy");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.CalculatorBO", false);
        Method methodInfo = new Method("add", "sum");
        methodInfo.addParameterInfo(new ParameterInfo("a", "100", "10"));
        methodInfo.addParameterInfo(new ParameterInfo("b", null, "10"));
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("sum"), Integer.valueOf(110));

        List list = actionInfo.getRuntimeInterceptors();
        Assert.assertEquals(Integer.valueOf(list.size()), Integer.valueOf(0));

        String type = actionInfo.getType();
        Assert.assertEquals(type, "dummy");
    }

    @SuppressWarnings("unchecked")
	@Test
    public void simpleServiceWithCacheExecute() throws Exception {
        actionInfo = new Action("dummy_action", "dummy");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.CalculatorBO", false);
        Method methodInfo = new Method("add", "sum");
        methodInfo.addParameterInfo(new ParameterInfo("a", "100", "10"));
        methodInfo.addParameterInfo(new ParameterInfo("b", null, "10"));
        methodInfo.setUseCache(true);
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("sum"), Integer.valueOf(110));

        List list = actionInfo.getRuntimeInterceptors();
        Assert.assertEquals(Integer.valueOf(list.size()), Integer.valueOf(0));

        String type = actionInfo.getType();
        Assert.assertEquals(type, "dummy");
    }


    /**
     * &lt;control&gt; 영역의 처리를 테스트. a, b 파라미터 중 a값이 클때 정상 수행
     *
     * @throws Exception
     */
    @Test
    public void simpleControlMethod() throws Exception {
        actionInfo = new Action("dummy_action");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.CalculatorBO", false);
        businessObjectInfo.setControlBlock(true);
        ServiceCondition condition = new ServiceCondition();
        condition.setCondition("a > b");
        Method methodInfo = new Method("add", "sum");
        methodInfo.addParameterInfo(new ParameterInfo("a", "100", "10"));
        methodInfo.addParameterInfo(new ParameterInfo("b", "99", "10"));
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("sum"), Integer.valueOf(199));

    }

    @Test
    public void simpleControl1Method() throws Exception {
        actionInfo = new Action("dummy_action");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.CalculatorBO", false);
        businessObjectInfo.setControlBlock(true);
        ServiceCondition condition = new ServiceCondition();
        condition.setCondition("a > b");
        Method methodInfo = new Method("add", "sum");
        methodInfo.addParameterInfo(new ParameterInfo("a", "100", "10"));
        methodInfo.addParameterInfo(new ParameterInfo("b", "99", "10"));
        businessObjectInfo.addMethodInfo(methodInfo);
        condition.addServiceInfo(businessObjectInfo);
        actionInfo.addControlBlockInfos(condition);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("sum"), Integer.valueOf(199));

    }

    public void chainMethodServiceExecute() throws Exception {
        actionInfo = new Action("dummy_action");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.CalculatorBO", false);
        Method methodInfo = new Method("add", "sum");
        methodInfo.addParameterInfo(new ParameterInfo("a", "100", "10"));
        methodInfo.addParameterInfo(new ParameterInfo("b", null, "10"));
        businessObjectInfo.addMethodInfo(methodInfo);
        methodInfo = new Method("divide", "result");
        methodInfo.addParameterInfo(new ParameterInfo("a", "1100", null));
        methodInfo.addParameterInfo(new ParameterInfo("b", "sum", null));
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("result"), Double.valueOf(10.0));
    }

    public void objectBasedMethodServiceExecute() throws Exception {
        actionInfo = new Action("dummy_action");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.PersonBO", false);
        Method methodInfo = new Method("getPerson", "person");
        methodInfo.addParameterInfo(new ParameterInfo("name", null, "Gildong,Hong"));
        methodInfo.addParameterInfo(new ParameterInfo("age", null, "20"));
        methodInfo.setUseCache(true);
        businessObjectInfo.addMethodInfo(methodInfo);
        methodInfo = new Method("getNameOfPersonObject", "nameOfPerson");
        methodInfo.addParameterInfo(new ParameterInfo("person.name", "person.name", null));
        methodInfo.addParameterInfo(new ParameterInfo("person.age", "person.age", null));
        businessObjectInfo.addMethodInfo(methodInfo);

        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("nameOfPerson"), "Gildong,Hong");

        resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("nameOfPerson"), "Gildong,Hong");
    }

    public void mapBasedMethodServiceExecute() throws Exception {
        actionInfo = new Action("dummy_action");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.PersonBO", false);
        Method methodInfo = new Method("getPerson", "person");
        methodInfo.addParameterInfo(new ParameterInfo("name", null, "Gildong,Hong"));
        methodInfo.addParameterInfo(new ParameterInfo("age", null, "20"));
        businessObjectInfo.addMethodInfo(methodInfo);
        methodInfo = new Method("getNameOfPersonMap", "nameOfPerson");
        methodInfo.addParameterInfo(new ParameterInfo("person", "person", null));
        businessObjectInfo.addMethodInfo(methodInfo);

        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("nameOfPerson"), "Gildong,Hong");
    }


    @Test
    public void returnNullhodInvoker() throws Exception {
        actionInfo = new Action("dummy_action");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.ReturnTestBO", false);
        Method methodInfo = new Method("returnNull", "nullValue");
        businessObjectInfo.addMethodInfo(methodInfo);

        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(null, dataMap.get("nullValue"));
    }

    @Test
    public void setNameMethodInvoker() throws Exception {
        actionInfo = new Action("dummy_action");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.ReturnTestBO", false);
        Method methodInfo = new Method("returnVoid", null);
        businessObjectInfo.addMethodInfo(methodInfo);

        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(0, dataMap.size());
    }

    @Test
    public void exceptionHandle() throws Exception {
        actionInfo = new Action("dummy_action");

        // action-service
        BusinessObject businessObjectInfo =
                new BusinessObject("com.nhncorp.usf.core.service.mock.MockExceptionBO", false);
        Method methodInfo = new Method("getUser", "ret_user");
        businessObjectInfo.addMethodInfo(methodInfo);

        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus;
        try {
            resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        } catch (Exception e) {
            resultStatus = RESULT_STATUS.failure;
        }

        Assert.assertNotNull(resultStatus);
        Assert.assertEquals(resultStatus, RESULT_STATUS.failure);
    }

    @SuppressWarnings("unchecked")
	@Test
    public void preScriptMethodServiceExecuteWithJS() throws Exception {
        actionInfo = new Action("dummy_action");
        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.PersonBO", false);
        Method methodInfo = new Method("getPerson", "person");
        methodInfo.addParameterInfo(new ParameterInfo("name", null, "Gildong,Hong"));
        methodInfo.addParameterInfo(new ParameterInfo("age", null, "10"));

        ScriptInfo scriptInfo = new ScriptInfo(ScriptEngineGlobals.JAVASCRIPT_SCRIPT_ENGINE_NAME, "replaceParams");
        scriptInfo.setCallType(null);

        Assert.assertEquals(scriptInfo.toString(), "javascript:replaceParams");

        methodInfo.addScriptInfo(scriptInfo);
        businessObjectInfo.addMethodInfo(methodInfo);

        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Assert.assertEquals(methodInfo.getParameterInfos().get(0).getDefaultValue(), "Gildong,Hong");

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);

        Map resultMap = (Map) dataMap.get("person");
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(resultMap.get("name"), "choco");    // name 은 choco
        Assert.assertEquals(resultMap.get("age"), Integer.valueOf(20));        // age 는 10 으로 parameter 값 수정
    }

    @SuppressWarnings("unchecked")
	@Test
    public void preScriptMethodServiceExecuteWithGroovy() throws Exception {
        actionInfo = new Action("dummy_action");
        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.PersonBO", false);
        Method methodInfo = new Method("getPerson", "person");
        methodInfo.addParameterInfo(new ParameterInfo("name", null, "Gildong,Hong"));
        methodInfo.addParameterInfo(new ParameterInfo("age", null, "10"));
        ScriptInfo scriptInfo = new ScriptInfo(ScriptEngineGlobals.GROOVY_SCRIPT_ENGINE_NAME, "replaceParams");
        scriptInfo.setCallType("prescript");
        methodInfo.addScriptInfo(scriptInfo);
        businessObjectInfo.addMethodInfo(methodInfo);

        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Map resultMap = (Map) dataMap.get("person");
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(resultMap.get("name"), "choco");   // name 은 choco
        Assert.assertEquals(resultMap.get("age"), Integer.valueOf(30));       // age sms 30 으로 parameter 값 수정
    }


    @Test
    public void postScriptMethodServiceExecuteWithJS1() throws Exception {
        actionInfo = new Action("dummy_action");
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.CalculatorBO", false);
        Method methodInfo = new Method("add", "sum");
        methodInfo.addParameterInfo(new ParameterInfo("a", "100", "10"));
        methodInfo.addParameterInfo(new ParameterInfo("b", null, "10"));
        ScriptInfo scriptInfo = new ScriptInfo(ScriptEngineGlobals.JAVASCRIPT_SCRIPT_ENGINE_NAME, "invokeCalculator");
        scriptInfo.setCallType("postscript");
        methodInfo.addScriptInfo(scriptInfo);
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("sum"), Double.valueOf(55.0));    // total 110에서 나누기 2
    }

    @Test
    public void postScriptMethodServiceExecuteWithGroovy1() throws Exception {
        actionInfo = new Action("dummy_action");
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.CalculatorBO", false);
        Method methodInfo = new Method("add", "sum");
        methodInfo.addParameterInfo(new ParameterInfo("a", "100", "10"));
        methodInfo.addParameterInfo(new ParameterInfo("b", null, "10"));
        ScriptInfo scriptInfo = new ScriptInfo(ScriptEngineGlobals.GROOVY_SCRIPT_ENGINE_NAME, "invokeCalculator");
        scriptInfo.setCallType("postscript");
        methodInfo.addScriptInfo(scriptInfo);
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("sum"), Integer.valueOf(22));  // total 110에서 나누기 5
    }

    @Test
    public void postScriptMethodServiceExecuteWithJS2() throws Exception {
        actionInfo = new Action("dummy_action");
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.ReplaceBO", false);
        Method methodInfo = new Method("getReturnMap", "stringValue");
        methodInfo.addParameterInfo(new ParameterInfo("userId", null, "choco"));
        ScriptInfo scriptInfo = new ScriptInfo(ScriptEngineGlobals.JAVASCRIPT_SCRIPT_ENGINE_NAME, "invokeMethod_2");
        scriptInfo.setCallType("postscript");
        methodInfo.addScriptInfo(scriptInfo);
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        log.debug(" postScriptMethodServiceExecuteWithJS2 returnValue : " + dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("stringValue"), "threetwoone");
    }

    @Test
    public void postScriptMethodServiceExecuteWithGroovy2() throws Exception {
        actionInfo = new Action("dummy_action");
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.ReplaceBO", false);
        Method methodInfo = new Method("getReturnMap", "stringValue");
        methodInfo.addParameterInfo(new ParameterInfo("userId", null, "choco"));
        ScriptInfo scriptInfo = new ScriptInfo(ScriptEngineGlobals.GROOVY_SCRIPT_ENGINE_NAME, "testMethod_2");
        scriptInfo.setCallType("postscript");
        methodInfo.addScriptInfo(scriptInfo);
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        log.debug(" postScriptMethodServiceExecuteWithGroovy2 returnValue : " + dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("stringValue"), "threeonetwo");
    }

    @SuppressWarnings("unchecked")
	@Test
    public void postScriptMethodServiceExecuteWithJS3() throws Exception {
        actionInfo = new Action("dummy_action");
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.ReplaceBO", false);
        Method methodInfo = new Method("getReturnArray", "arrayValue");
        methodInfo.addParameterInfo(new ParameterInfo("userId", null, "choco"));
        ScriptInfo scriptInfo = new ScriptInfo(ScriptEngineGlobals.JAVASCRIPT_SCRIPT_ENGINE_NAME, "testMethod_5");
        scriptInfo.setCallType("postscript");
        methodInfo.addScriptInfo(scriptInfo);
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        log.debug("  postScriptMethodServiceExecuteWithJS3 arrayValue : " + dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        ArrayList arrayList = (ArrayList) dataMap.get("arrayValue");
        log.debug(" arrayList.get(0) : " + arrayList.get(0));
        log.debug(" arrayList.get(1) : " + arrayList.get(1));
        Assert.assertEquals(arrayList.get(0), "gammabetaalpha");
        Assert.assertEquals(arrayList.get(1), 6.0);
    }

    @SuppressWarnings("unchecked")
	@Test
    public void postScriptMethodServiceExecuteWithGroovy3() throws Exception {
        actionInfo = new Action("dummy_action");
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.ReplaceBO", false);
        Method methodInfo = new Method("getReturnArray", "arrayValue");
        methodInfo.addParameterInfo(new ParameterInfo("userId", null, "choco"));

        ScriptInfo scriptInfo = new ScriptInfo(ScriptEngineGlobals.GROOVY_SCRIPT_ENGINE_NAME, "testMethod_5");
        scriptInfo.setCallType("postscript");
        methodInfo.addScriptInfo(scriptInfo);
        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        log.debug(" postScriptMethodServiceExecuteWithGroovy3 arrayValue : " + dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        ArrayList arrayList = (ArrayList) dataMap.get("arrayValue");
        log.debug(" arrayList.get(0) : " + arrayList.get(0));
        log.debug(" arrayList.get(1) : " + arrayList.get(1));

        Assert.assertEquals(arrayList.get(0), "gammaalphabeta");
        Assert.assertEquals(arrayList.get(1), 6);
    }

    @Test
    public void parameterGeneratorTest() throws Exception {
        actionInfo = new Action("dummy_action", "dummy");

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.PersonBO", false);
        Method methodInfo = new Method("getPerson", "person");
        methodInfo.addParameterInfo(new ParameterInfo("parameterMap", null, null));
        methodInfo.setIsParameterMap(true);

        businessObjectInfo.addMethodInfo(methodInfo);
        actionInfo.addBusinessObjectInfo(businessObjectInfo);

        Map<String, Object> requestParam = new HashMap<String, Object>();
        requestParam.put("name", "Gildong,Hong");
        requestParam.put("age", "100");

        ServiceExecutionContext context = new ServiceExecutionContext(new HashMap<String, Object>());
        context.setAction(actionInfo);
        context.setBo(businessObjectInfo);
        context.setMethod(methodInfo);
        context.setAttribute("requestParam", requestParam);

        PreSupplement parameterGenerator = new ParameterGenerator();

        Assert.assertTrue(parameterGenerator.beforeExecute(context));
        Assert.assertNotNull(context.getMethodParameter());
    }

    @SuppressWarnings("deprecation")
	@Test
    public void simpleBOGroupServiceExecute() throws Exception {
        actionInfo = new Action("dummy_action", "dummy");

        ScriptInfo scriptInfo = new ScriptInfo(ScriptEngineGlobals.JAVASCRIPT_SCRIPT_ENGINE_NAME, "replaceValue");
        scriptInfo.setParams("choco");
        scriptInfo.setReturnId("name");
        actionInfo.setResultCommonScriptInfo(scriptInfo);

        ScriptInfo groupscriptInfo = new ScriptInfo(ScriptEngineGlobals.JAVASCRIPT_SCRIPT_ENGINE_NAME, "getBOList");
        BusinessObjectGroup boGroup = new BusinessObjectGroup(groupscriptInfo);

        // action-service
        BusinessObject businessObjectInfo = new BusinessObject("com.nhncorp.usf.core.service.mock.HelloBO", false);
        Method methodInfo = new Method("hello", "name");
        methodInfo.addParameterInfo(new ParameterInfo("name", null, "choco"));
        businessObjectInfo.addMethodInfo(methodInfo);
        boGroup.addBoInfos(businessObjectInfo);
        actionInfo.addBoGroupInfos(boGroup);

        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("name", "naver");
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
        Assert.assertEquals(dataMap.get("name"), "naver");
    }

    @Test
    public void emptyServiceExecute() throws Exception {
        actionInfo = new Action("dummy_action", "dummy");

        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("name", "naver");
        RESULT_STATUS resultStatus = serviceExecutor.execute(actionInfo, dataMap);
        Assert.assertEquals(resultStatus, RESULT_STATUS.success);
    }
}
